package org.p2psimulator.entity.behavior;

import java.util.HashSet;

import org.p2psimulator.entity.Id;
import org.p2psimulator.entity.Message;
import org.p2psimulator.entity.reputation.Reputation;
import org.p2psimulator.entity.reputation.ReputationRepository;

public class EvilBehavior extends Behavior {

    private HashSet<Id> evilSet = new HashSet<Id>();

    @Override
    public boolean answareMessage(Id rater, Message msg) {
        interacitonCount++;
        return true;
    }

    @Override
    public BehaviorTypes getBehaviorType() {
        return BehaviorTypes.EVIL;
    }

    @Override
    public boolean rate() {
        return true;
    }

    @Override
    public double rateResource(Id rater, Id resource,
            ReputationRepository repository) {
        try {
            Reputation reputation = repository.getLocalReputation(rater,
                    resource);
            if (reputation != null) {
                return reputation.getNormalReputation();
            } else {
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public double ratePeer(Id rater, Id ratee, BehaviorTypes rateeBehavior,
            ReputationRepository repository) {
        if (rateeBehavior == BehaviorTypes.EVIL) {
            if (evilSet.contains(ratee)) {
                return 1;
            } else {
                evilSet.add(ratee);
                return 0;
            }
        } else {
            return 0;
        }
    }

    @Override
    public double rateTransaction(Id rater, Id ratee, int transResult,
            BehaviorTypes rateeBehavior, ReputationRepository repository) {

        interacitonCount++;
        if (rateeBehavior == BehaviorTypes.EVIL) {
            if (evilSet.contains(ratee)) {
                return 1;
            } else {
                evilSet.add(ratee);
                return 0;
            }
        } else {
            return 0;
        }
    }

    @Override
    public boolean damageResource() {
        return false;
    }

    @Override
    public boolean transacNeutro(BehaviorTypes rateeBehavior, Id ratee) {

        if (rateeBehavior == BehaviorTypes.EVIL) {
            if (evilSet.contains(ratee)) {
                return false;
            } else {
                evilSet.add(ratee);
                return true;
            }
        } else {
            return true;
        }
    }
}
